import {postAction} from '@/request/BaseRequest';
import {ElMessageBox} from 'element-plus';

export default {
  approve(approveData) {
    return new Promise((success, err) => {
      postAction('/plat/flow/approve', approveData).then(data => {
        if (data.success) {
          success();
        }
      });
    });
  },
  end(data) {
    return new Promise((success, err) => {
      ElMessageBox.confirm('流程结束后无法回退, 确定要结束流程?', '确定结束流程?', {
        draggable: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        postAction('/flow/end', data).then(data => {
          if (data.success) {
            success();
          }
        });
        success();
      }).catch(() => {
      });
    });
  },
  toFormUserTask(commentData) {
    return new Promise((success, err) => {
      ElMessageBox.confirm('回退给流程发起人?', '确定退回?', {
        draggable: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        postAction('/flow/toForm', commentData).then(data => {
          if (data.success) {
            success();
          }
        });
      }).catch(() => {
      });
    });
  },
  changeFlow(data, before) {
    return new Promise((success, err) => {
      ElMessageBox.confirm('修改流程后不可回退', '确定修改吗', {
        draggable: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        if(before) before();
        postAction('/flow/changeFlow', data).then(data => {
          if (data.success) {
            success();
          }
        });
      }).catch(() => {
      });
    });
  },
  userChange(data) {
    return new Promise((success, err) => {
      ElMessageBox.confirm('转办后不可回退', '确认转办吗?', {
        draggable: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        postAction('/flow/changeUser', data).then(() => {
          success();
        }).catch(() => {
        });
      }).catch(() => {
      });
    });
  },
  toRollbackNode(data) {
    return new Promise((success, err) => {
      ElMessageBox.confirm('回退后不可恢复', '确认回退吗?', {
        draggable: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        postAction('/flow/toRollbackNode', data).then(() => {
          success();
        }).catch(() => {
        });
      }).catch(() => {
      });
    });
  },
  flowDescChange(data) {
    return new Promise((success, err) => {
      postAction('/flow/attachmentEdit', data).then(data => {
        if (data.success) {
          success();
        }
      });
    });
  },
  changeUser(data) {
    return new Promise((success, err) => {

    });
  },
};


export const actionTypes = {
  approve: {
    type: 'approve',
    name: '同意'
  },
  changeUser: {
    type: 'changeUser',
    name: '转办'
  },
  toRollbackNode: {
    type: 'toRollbackNode',
    name: '退回到指定节点'
  },
  attachmentEdit: {
    type: 'attachmentEdit',
    name: '修改附件'
  },
  toForm: {
    type: 'toForm',
    name: '退回到发起人'
  },
  end: {
    type: 'end',
    name: '不通过'
  },
  changeFlow: {
    type: 'changeFlow',
    name: '修改流程'
  },
};
